package cn.rongcloud.im.ui.moudle.convenient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;

import cn.rongcloud.im.R;
import cn.rongcloud.im.ui.moudle.convenient.bean.ConvenientStoreGroupBean;

/**
 * Created by lincheng on 2017/3/15.
 */

public class ConvenientStoreGroupAdapter extends RecyclerView.Adapter<ConvenientStoreGroupAdapter.MyViewHolder>{
    /**上下文*/
    private Context mContext;
    /**包含组Bean的List*/
    private List<ConvenientStoreGroupBean> mGroupBeanList;
    /**组点击的监听事件*/
    private OnGroupClickListener mOnGroupClickListener;

    /**
     * 构造函数
     * @param context     上下文
     * @param groupBeanList     包含组Bean的List
     */
    public ConvenientStoreGroupAdapter(Context context ,List<ConvenientStoreGroupBean> groupBeanList){
        mContext = context;
        mGroupBeanList =groupBeanList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_convenient_store_group, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        String groupName = mGroupBeanList.get(position).getGroupName();
        boolean isClick = mGroupBeanList.get(position).isGroupClicked();
        holder.mGroupNameTextView.setText(groupName);
        setGroupLayoutClickListener(holder , position);
        setGroupClickViewChange(holder ,isClick);
    }

    /**
     * 设置组的点击时候View的变化
     * @param holder    MyViewHolder对象
     * @param isClick    是否点击
     */
    private void setGroupClickViewChange(MyViewHolder holder, boolean isClick) {
        if(isClick){
            holder.mGroupClickBarTextView.setVisibility(View.VISIBLE);
            holder.mGroupNameTextView.setTextColor(mContext.getResources().getColor(R.color.deep_green_color));
            holder.mGroupNameTextView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }else{
            holder.mGroupClickBarTextView.setVisibility(View.INVISIBLE);
            holder.mGroupNameTextView.setTextColor(mContext.getResources().getColor(R.color.deep_gray_color2));
            holder.mGroupNameTextView.setBackgroundColor(mContext.getResources().getColor(R.color.light_gray_color1));
        }
    }

    /**
     * 设置组的点击事件
     * @param holder    MyViewHolder对象
     * @param position    当前位置
     */
    private void setGroupLayoutClickListener(MyViewHolder holder, int position) {
        holder.mContentLayout.setTag(position);

        if(null != mOnGroupClickListener){
            holder.mContentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = (int) view.getTag();
                    mOnGroupClickListener.onClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mGroupBeanList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        /**外层的Layout布局*/
        private LinearLayout mContentLayout;
        /**组的名字*/
        TextView mGroupNameTextView;
        /**组内左边的绿色bar，当点击的时候显示*/
        TextView mGroupClickBarTextView;
        public MyViewHolder(View itemView) {
            super(itemView);
            mGroupNameTextView = (TextView) itemView.findViewById(R.id.tv_group_item);
            mGroupClickBarTextView = (TextView) itemView.findViewById(R.id.tv_click_bar);
            mContentLayout = (LinearLayout) itemView.findViewById(R.id.content_layout);
        }
    }

    /**
     * 组点击的接口
     */
    public interface OnGroupClickListener{
        /**组点击函数*/
        void onClick(int position);
    }

    /**
     * 设置组的点击事件
     * @param listener    外部传进来的组的点击事件
     */
    public void setOnGroupClickListener(OnGroupClickListener listener){
        mOnGroupClickListener = listener;
    }
}
